package com.eoner.baselibrary.base;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.eoner.baselib.utils.sp.SPUtils;
import com.eoner.common.config.Config;
import com.eoner.baselibrary.widget.loading.ProgressDialog;
import com.eoner.baselib.utils.other.TUtil;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.eoner.baselib.application.BaseApplication;

import java.io.Serializable;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 作者：sean
 * 创建时间：2017/6/7 16:22
 * 功能说明：
 */

public abstract class IBaseFragment<T extends BasePresenter> extends Fragment {

    protected Context mContext;
    public View mRootView;
    Unbinder unbinder;
    protected final static String PAGE_SIZE = "20";

    public T mPresenter;
    public ProgressDialog pDialog;
    private boolean isFirst = true;
    private static final String STATE_SAVE_IS_HIDDEN = "STATE_SAVE_IS_HIDDEN";
    public Typeface iconfont;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mContext = getActivity();
        if (mRootView == null) {
            mRootView = getRootView(inflater);
        }

        iconfont = Typeface.createFromAsset(mContext.getAssets(), "fonts/iconfont.ttf");
        mPresenter = TUtil.getT(this, 0);
        if (this instanceof BaseView) mPresenter.setVM(this);

        unbinder = ButterKnife.bind(this, mRootView);
        if (isFirst) {
            pDialog = new ProgressDialog(mContext);
            initOnlyOnce();
            initOnlyOnce(mRootView);
            isFirst = false;
        }
        initView(mRootView);
        return mRootView;
    }

    protected void initOnlyOnce() {
    }

    protected void initOnlyOnce(View rootView) {

    }

    public void hideSoftInput(EditText et) {
        InputMethodManager inputManager = (InputMethodManager) et.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(et.getWindowToken(), 0);
    }

    public void showSoftInput(EditText et) {
        et.setFocusable(true);
        et.setFocusableInTouchMode(true);
        et.requestFocus();

        InputMethodManager inputManager = (InputMethodManager) et.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.showSoftInput(et, 0);
    }

    protected abstract View getRootView(LayoutInflater inflater);

    protected void initView(View mRootView) {
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (unbinder != null) {
            unbinder.unbind();
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        if (mPresenter != null) {
            mPresenter.onDestroy();
        }
    }

    public boolean isLogin() {
        return !TextUtils.isEmpty(SPUtils.getInstance().getString(Config.CUSTOMER_TOKEN));
    }

    public void showToast(String str) {
        //Toast.makeText(mContext, str, Toast.LENGTH_SHORT).show();
        Toast toast = Toast.makeText(mContext,str, Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }

    public void startActivitryWithBundle(@NonNull Class clazz, String[][] val) {
        Intent intent = new Intent(mContext, clazz);
        Bundle bundle = new Bundle();
        if (val != null) {
            for (String[] aVal : val) {
                bundle.putString(aVal[0], aVal[1]);
            }
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    /**
     * 参数传递(注：该方法为了方便传参,参数类型有局限性,传递特殊类型时单独构建Intent) author：FuZhi
     **/
    public void goTo(Class<?> goToCls, Object... values) {
        goTo(false, goToCls, values);
    }

    // <editor-fold defaultstate="collapsed" desc="参数传递(注：该方法为了方便传参,参数类型有局限性,传递特殊类型时单独构建Intent)">
    public void goTo(boolean finish, Class<?> goToCls, Object... values) {
        Intent intent = new Intent(getActivity(), goToCls);
        int i = 0;
        for (Object value : values) {
            Class<?> cls = value.getClass();
            String key = String.valueOf(i);
            if (cls == String.class) {
                intent.putExtra(key, (String) value);
            } else if (value instanceof Serializable) {
                intent.putExtra(key, (Serializable) value);
            } else if (cls == int[].class) {
                intent.putExtra(key, (int[]) value);
            } else if (cls == long[].class) {
                intent.putExtra(key, (long[]) value);
            } else if (cls == boolean[].class) {
                intent.putExtra(key, (boolean[]) value);
            } else if (cls == float[].class) {
                intent.putExtra(key, (float[]) value);
            } else if (cls == double[].class) {
                intent.putExtra(key, (double[]) value);
            } else if (cls == char[].class) {
                intent.putExtra(key, (char[]) value);
            } else if (cls == String[].class) {
                intent.putExtra(key, (String[]) value);
            } else if (cls == Bundle.class) {
                intent.putExtra(key, (Bundle) value);
            } else {
                throw new RuntimeException("不支持参数类型: " + cls.getSimpleName());
            }
            i++;
        }
        startActivity(intent);
        if (finish) {
            getActivity().finish();
        }
    }
    // </editor-fold>

    /**
     * 非空判断
     **/
    public static boolean isEmpty(Object... objs) {
        for (Object obj : objs) {
            if (obj == null) {
                return true;
            } else if (obj instanceof String && obj.equals("")) {
                return true;
            }
        }
        return false;
    }

    public static void VISIBLE(View view) {
        if (view != null) {
            view.setVisibility(View.VISIBLE);
        }
    }

    public static void INVISIBLE(View view) {
        if (view != null) {
            view.setVisibility(View.INVISIBLE);
        }
    }

    public static void GONE(View view) {
        if (view != null) {
            view.setVisibility(View.GONE);
        }
    }

    /**
     * 显示Toast信息
     **/
    public void T(Object s) {
        ToastUtil.showToast(String.valueOf(s));
    }

    public boolean onBackpressed() {
        //TODO:区别2
        List<Fragment> fragments = getChildFragmentManager().getFragments();
        if (null != fragments && !fragments.isEmpty()) {
            for (Fragment fragment : fragments) {
                if (fragment.isResumed() && fragment.getUserVisibleHint() && fragment instanceof IBaseFragment) {
                    IBaseFragment f = (IBaseFragment) fragment;
                    if (f.onBackpressed()) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
}
